﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using FCModel;
using FCDAL;
using System.Data;
using System.Data.OleDb;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using MiscLib;

namespace FCBLL
{
    public class TableFormatBOMBLL


    {

        string logPath = "D:\\Convert.log";
        IList<string> Value_Strs_List = new List<string>();
        //增加标题行和值行的定义在testype
        int iHeadIndex = 0;
        int iValueIndex =0;
        string iDataFieldIdx = "";
        string iTopBottom = "";
        string iBomType = "";
        string icustomermark = "";
        string iuserdefine = "";
        string iuserdefine1 = "";
        string iuserdefine2 = "";
        string iuserdefine3 = "";
        string iuserdefine4 = "";
        string iuserdefine5 = "";
        string iuserdefine6 = "";
        string iuserdefine7 = "";
        string iuserdefine8 = "";
        string iuserdefine9 = "";
        string iuserdefine10 = "";
        string iuserdefine11 = "";
        string iuserdefine12 = "";
        string iuserdefine13 = "";
        string iuserdefine14 = "";
        string iuserdefine15 = ""; 
        string iuserdefine16 = "";
        protected BOM_Head bomHead = new BOM_Head();
        IList<string> arrSourceFile_New = new List<string>();
        //protected bas_TestParamDao basTestParamDataTable = new bas_TestParamDao();
        public void Process_File(FileInfo fi)
        {
            Value_Strs_List = File.ReadAllLines(fi.FullName).ToList<string>();
        }

        public IList<BOMParameterDao> AnalysisFile(string[] arrSourceFile,string[] arrPOSFile, string BOMType)
        {
            //初始化ValueIndex和HeadIndex
            BOM_Head btt = CommonBLL.getBOM_HeadByBOMType(BOMType);
            iBomType=string.IsNullOrWhiteSpace(btt.BOMType.ToString()) ? iBomType : btt.BOMType;
            iTopBottom=string.IsNullOrWhiteSpace(btt.TopBottom.ToString()) ? iTopBottom : btt.TopBottom;
            iDataFieldIdx = string.IsNullOrWhiteSpace(btt.DataFieldIdx.ToString()) ? iDataFieldIdx : btt.DataFieldIdx;
            iHeadIndex = string.IsNullOrWhiteSpace(btt.RowStartIdx.ToString()) ? iHeadIndex : (Int32)btt.RowStartIdx;
            iValueIndex = string.IsNullOrWhiteSpace(btt.ValueStartIdx.ToString()) ? iValueIndex : (Int32)btt.ValueStartIdx;
            iuserdefine = string.IsNullOrWhiteSpace(btt.SpecialFlag.ToString()) ? iuserdefine : btt.SpecialFlag;
            string[] ArrIuserdefine = iuserdefine.Split(':');
            iuserdefine1 = ArrIuserdefine[0];
            iuserdefine2 = ArrIuserdefine[1];
            iuserdefine3 = ArrIuserdefine[2];
            iuserdefine4 = ArrIuserdefine[3];
            iuserdefine5 = ArrIuserdefine[4];
            //TestparamDAL tpDAL = new TestparamDAL();
            IList<BOMParameterDao> listSSD = new List<BOMParameterDao>();
            //12个
            IQueryable<BOM_Detail> listBT = new BaseRepositoryDAL<BOM_Detail>().LoadEntities(p => p.BOMType.Equals(BOMType, StringComparison.OrdinalIgnoreCase)).OrderBy(P => P.Seq);
            //listBT.OrderBy<BOM_Detail, int>(P => P.Seq);
            //listBT.OrderBy(P => P.Seq);

          //  string sequence = new BaseRepositoryDAL<bas_TestType>().LoadEntities(p => p.testType.Equals(BOMType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().dataFieldIndex;
            //tpDAL.GetTestParamBytestType(fileType);

            int iSeqNo = 1;
            string rowData;
            string TOPBOTTOMValue = "";
            int topCount = 1;
            int BottomCount = 1;

            for (int i = iValueIndex; i < arrSourceFile.Length; i++)
            {
                List<string> list = arrSourceFile.ToList();
                rowData = arrSourceFile[i];
                if (string.IsNullOrWhiteSpace(rowData.Trim()))
                {
                    continue;
                }
                BOMParameterDao pd = new BOMParameterDao();
                string[] temArrData = rowData.Split(';');
                string StoptemArrData = temArrData[1];
                if (StoptemArrData == "")
                {
                    break;
                }
                pd.SeqNo = iSeqNo.ToString();

                foreach (BOM_Detail bt in listBT.Where(p => !string.IsNullOrWhiteSpace(p.Express)))
                {
                    BOMParameter ss = new BOMParameter();
                    ss.ColSeqNo = bt.Seq;
                    ss.ParameterName = bt.FieldName;
                    string exp = bt.Express;
                    string [] expArr = exp.Split(';');
                    string[] dtopbottom = iTopBottom.Split(';');
                    string dtop = dtopbottom[0].Split(':')[0];
                    string dbottom = dtopbottom[1].Split(':')[0];
                    string Oldcolumn = "";
                    int columnidxFind = -1;

                    //FIND模式
                    try
                    {
                        if (expArr.Contains("FIND"))
                        {
                            //temArrData = arrSourceFile[i].Split(';');
                            var columnInfo = expArr.Where(p => p.StartsWith("FIND[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define FieldName");
                            string columnInfo1 = columnInfo.Replace("FIND[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("FIELDNAME")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define FieldName");
                            if (columnInfo2.Length > 1 && columnInfo2[1].Contains("PREFIX"))
                            {
                                string columnInfo4 = columnInfo2[1].Split(':')[1];
                                Oldcolumn = columnInfo2[0].Split(':')[1];
                                //int columnidx = -1;
                                string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                                for (int j = 0; j < headindex.Length; j++)
                                {
                                    if (headindex[j] == Oldcolumn)
                                    {
                                        columnidxFind = j;
                                        break;
                                    }
                                }
                                ss.ParameterValue = columnInfo4 + temArrData[columnidxFind];
                            }
                            //组件数量
                            //else if (columnInfo2.Length > 1 && columnInfo2[1].Contains("DEFAULT"))
                            //{
                            //    string columnInfo4 = columnInfo2[1].Split(':')[1];
                            //    Oldcolumn = columnInfo2[0].Split(':')[1];
                            //    //int columnidx = -1;
                            //    string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                            //    for (int j = 0; j < headindex.Length; j++)
                            //    {
                            //        if (headindex[j] == Oldcolumn)
                            //        {
                            //            columnidxFind = j;
                            //            break;
                            //        }
                            //    }
                            //    if (temArrData[columnidxFind] == "")
                            //    {
                            //        ss.ParameterValue = columnInfo4;
                            //    }
                            //    else
                            //    {
                            //        ss.ParameterValue = temArrData[columnidxFind];
                            //    }
                            //}
                            else
                            {
                                Oldcolumn = columnInfo1.Split(':')[1];
                                //int columnidx = -1;
                                string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                                for (int j = 0; j < headindex.Length; j++)
                                {
                                    if (headindex[j] == Oldcolumn)
                                    {
                                        columnidxFind = j;
                                        break;
                                    }
                                }
                                ss.ParameterValue = temArrData[columnidxFind];
                            }

                        }
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                    }
                    //FIXED模式
                    try
                    {
                        if (expArr.Contains("FIXED"))
                        {
                            var columnInfo = expArr.Where(p => p.StartsWith("FIXED[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define FIXEDVALEU");
                            string columnInfo1 = columnInfo.Replace("FIXED[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("FIXEDVALEU")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define FIXEDVALEU");
                             Oldcolumn = columnInfo1.Split(':')[1];
                            if (Oldcolumn == "NULL")
                            {
                                ss.ParameterValue = "";
                            }
                            else
                            {
                                ss.ParameterValue = Oldcolumn;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    //Increment模式
                    try
                    {
                        if (expArr.Contains("Increment"))
                        {

                            var columnInfo = expArr.Where(p => p.StartsWith("Increment[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define Num");
                            string columnInfo1 = columnInfo.Replace("Increment[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("Num")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define Num");
                            //string Oldcolumn = columnInfo1.Split(':')[1];
                            int IncrementNum = Convert.ToInt32(columnInfo2[0].Split(':')[1]);
                            int IncrementLength = Convert.ToInt32(columnInfo2[1].Split(':')[1]);
                            char IncrementPadChar = Convert.ToChar(columnInfo2[2].Split(':')[1]);
                            //int innum = IncrementNum * (i - iHeadIndex);
                            int innum = 1;
                            string AreaValue = "";

                            AreaValue = TOPBOTTOMValue;
                            if (dtop.Equals(AreaValue.ToUpper()))
                            {
                                innum = IncrementNum * topCount;
                                topCount++;
                            }
                            else
                            {
                                innum = IncrementNum * BottomCount;
                                BottomCount++;
                            }

                            string innumvalue = "";
                            if (Convert.ToString(innum).Length < IncrementLength)
                            {
                                innumvalue = Convert.ToString(innum).PadLeft(IncrementLength, IncrementPadChar);
                            }
                            else
                            {
                                innumvalue = Convert.ToString(innum);
                            }
                            ss.ParameterValue = innumvalue;
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    //COUNT模式
                    try
                    {
                        if (expArr.Contains("COUNT"))
                        {
                            //temArrData = arrSourceFile[i].Split(';');
                            var columnInfo = expArr.Where(p => p.StartsWith("COUNT[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define FieldName");
                            string columnInfo1 = columnInfo.Replace("COUNT[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("FIELDNAME")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define FieldName");

                            Oldcolumn = columnInfo1.Split(':')[1];
                            //int columnidx = -1;
                            string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                            for (int j = 0; j < headindex.Length; j++)
                            {
                                if (headindex[j] == Oldcolumn)
                                {
                                    columnidxFind = j;
                                    break;
                                }
                            }
                            if (temArrData[columnidxFind] == "")
                            {
                                ss.ParameterValue = "0";
                            }
                            else
                            {
                                ss.ParameterValue = temArrData[columnidxFind].Split(',').Length.ToString();
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    //REPLACE模式
                    try
                    {
                        if (expArr.Contains("REPLACE"))
                        {
                            var columnInfo = expArr.Where(p => p.StartsWith("REPLACE[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define MARK");
                            string columnInfo1 = columnInfo.Replace("REPLACE[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("MARK")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define MARK");

                             Oldcolumn = columnInfo2[0].Split(':')[1];
                            string Newcolumn = columnInfo2[2].Split(':')[1];
                            int columnidx = -1;
                            int ReplaceNO = -1;
                            string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                            string ReplacerowData = arrSourceFile[i - 1];
                            string[] ReplaceArrData = ReplacerowData.Split(';');
                            string ReplacerowDataSecond = arrSourceFile[i - 2];
                            string[] ReplaceArrDataSecond = ReplacerowDataSecond.Split(';');
                            //temArrData = arrSourceFile[i].Split(';');
                            for (int j = 0; j < headindex.Length; j++)
                            {
                                if (headindex[j] == Oldcolumn)
                                {
                                    columnidx = j;
                                    break;
                                }
                            }
                            for (int k = 0; k < headindex.Length; k++)
                            {
                                if (headindex[k] == Newcolumn)
                                {
                                    ReplaceNO = k;
                                    break;
                                }
                            }
                            if (temArrData[columnidx] == "" && (ReplaceArrData[columnidx] == "A" || ReplaceArrData[columnidx] == "X"))
                            {
                                //arrSourceFile[i-1][columnidx]
                                ss.ParameterValue = iuserdefine5 + ReplaceArrData[ReplaceNO];
                            }
                            else if (temArrData[columnidx] == "" && ReplaceArrData[columnidx] != "A" && ReplaceArrData[columnidx] != "X")
                            {
                                ss.ParameterValue = iuserdefine5 + ReplaceArrDataSecond[ReplaceNO];
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    //TOPBOTTOM模式
                    //try
                    //{
                    //    if (expArr.Contains("TOPBOTTOM"))
                    //    {
                    //        var columnInfo = expArr.Where(p => p.StartsWith("TOPBOTTOM[")).FirstOrDefault();
                    //        if (columnInfo == null) throw new Exception("must define FieldName");
                    //        string columnInfo1 = columnInfo.Replace("TOPBOTTOM[", "").Replace("]", "");
                    //        string[] columnInfo2 = columnInfo1.Split(',');
                    //        var columnInfo3 = columnInfo2.Where(p => p.StartsWith("FIELDNAME")).FirstOrDefault();
                    //        if (columnInfo3 == null) throw new Exception("must define FieldName");
                    //         Oldcolumn = columnInfo2[0].Split(':')[1];
                    //        string Newcolumn = columnInfo2[1].Split(':')[1];
                    //        int columnidx = -1;// arrSourceFile[iHeadIndex].Split(',');
                    //        string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                    //        string[] TOPBOTTOM = arrPOSFile[iHeadIndex].Split(';');
                    //        for (int j = 0; j < headindex.Length; j++)
                    //        {
                    //            if (headindex[j] == Oldcolumn)
                    //            {
                    //                columnidx = j;
                    //                break;
                    //            }
                    //        }
                    //        string[] columnvalue = temArrData[columnidx].Split(',');
                    //        string ColumnONE = columnvalue[0];
                    //        var aa  = arrSourceFile.ToList<string>() ;
                    //        aa.Add();
                    //        for (int u=0; u < columnvalue.Length; u++)
                    //        {

                    //            for (int k = 1; k < arrPOSFile.Length; k++)
                    //            {
                    //                if (arrPOSFile[k].Contains(ColumnONE))
                    //                {
                    //                    string temp = arrPOSFile[k];
                    //                    TOPBOTTOMValue = temp.Split(';')[1].ToUpper();
                    //                    break;
                    //                }
                    //            }
                    //        }
                    //        ss.ParameterValue = TOPBOTTOMValue;
                    //    }
                    //}
                    //catch (Exception ex)
                    //{
                    //    ex.ToString();
                    //}

                    //TOPBOTTOM模式
                    try
                    {
                        if (expArr.Contains("TOPBOTTOM"))
                        {
                            var columnInfo = expArr.Where(p => p.StartsWith("TOPBOTTOM[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define FieldName");
                            string columnInfo1 = columnInfo.Replace("TOPBOTTOM[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("FIELDNAME")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define FieldName");
                            Oldcolumn = columnInfo2[0].Split(':')[1];
                            string Newcolumn = columnInfo2[1].Split(':')[1];
                            int columnidx = -1;// arrSourceFile[iHeadIndex].Split(',');
                            string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                            //string[] TOPBOTTOM = arrPOSFile[iHeadIndex].Split(';');
                            for (int j = 0; j < headindex.Length; j++)
                            {
                                if (headindex[j] == Oldcolumn)
                                {
                                    columnidx = j;
                                    break;
                                }
                            }
                            string[] columnvalue = temArrData[columnidx].Split(',');

                            Dictionary<string, string> dic = new Dictionary<string, string>();
                            for (int u = 0; u < columnvalue.Length; u++)
                            {
                                for (int k = 1; k < arrPOSFile.Length; k++)
                                {
                                    if (columnvalue[u] == "")
                                    {
                                        TOPBOTTOMValue = listSSD[listSSD.Count-1].listSSD[11].ParameterValue;
                                        break;
                                    }
                                    if (arrPOSFile[k].Contains(columnvalue[u]) && columnvalue[u]!="")
                                    {
                                        if (!dic.ContainsKey(arrPOSFile[k].Split(';')[1]))
                                        {
                                            dic.Add(arrPOSFile[k].Split(';')[1], arrPOSFile[k].Split(';')[0]);
                                        }
                                        else
                                        {
                                            dic[arrPOSFile[k].Split(';')[1]] = dic[arrPOSFile[k].Split(';')[1]] + "," + arrPOSFile[k].Split(';')[0];
                                        }
                                        if (dic.Count == 1)
                                        {
                                            string temp = arrPOSFile[k];
                                            TOPBOTTOMValue = temp.Split(';')[1].ToUpper();
                                            break;
                                        }
                                    }
                                }
                            }
                            if (dic.Count >1)
                            {

                                foreach (var dd in dic)
                                {
                                    string rowData_Tmp = rowData;
                                    temArrData = arrSourceFile[i].Split(';');
                                    string data = temArrData[columnidx];
                                    rowData_Tmp = rowData_Tmp.Replace(data, dd.Value);
                                    TOPBOTTOMValue = dd.Key;
                                    list.Insert(i+1, rowData_Tmp);
                                    //list.Add(rowData_Tmp);
                                    //arrSourceFile_New.Add(rowData_Tmp);
                                }

                                list.RemoveAt(i);
                                arrSourceFile = list.ToArray();
                                for (int l = i+1; l < arrSourceFile.Length; l++)
                                {
                                    if (arrSourceFile[l + 1].Split(';')[columnidx].ToString() == "")
                                    {
                                        list.Insert(l, arrSourceFile[l + 1]);
                                        //list.Add(arrSourceFile[i + 1]);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                arrSourceFile = list.ToArray();
                            }

                            ss.ParameterValue = TOPBOTTOMValue;
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    //INPUT模式
                    try
                    {
                        if (expArr.Contains("INPUT"))
                        {
                            var columnInfo = expArr.Where(p => p.StartsWith("TOPBOTTOM[")).FirstOrDefault();
                            if (columnInfo == null) throw new Exception("must define FieldName");
                            string columnInfo1 = columnInfo.Replace("TOPBOTTOM[", "").Replace("]", "");
                            string[] columnInfo2 = columnInfo1.Split(',');
                            var columnInfo3 = columnInfo2.Where(p => p.StartsWith("FIELDNAME")).FirstOrDefault();
                            if (columnInfo3 == null) throw new Exception("must define FieldName");
                             Oldcolumn = columnInfo2[0].Split(':')[1];
                            string Newcolumn = columnInfo2[1].Split(':')[1];
                            int columnidx = -1;// arrSourceFile[iHeadIndex].Split(',');
                            int fieldindex = -1;
                            string[] datafieldindex = arrSourceFile[Convert.ToInt32(iDataFieldIdx)].Split(';');
                            string[] headindex = arrSourceFile[iHeadIndex].Split(';');
                            string temp = "";
                            for (int j = 0; j < datafieldindex.Length; j++)
                            {
                                if (datafieldindex[j] == iuserdefine1)
                                {
                                    fieldindex = j + 1;
                                    break;
                                }
                            }
                            for (int m = 0; m < headindex.Length; m++)
                            {
                                if (headindex[m] == Oldcolumn)
                                {
                                    columnidx = m;
                                    break;
                                }
                            }
                            temArrData = arrSourceFile[i].Split(';');
                            string[] columnvalue = temArrData[columnidx].Split(',');
                            string ColumnONE = columnvalue[0];
                            for (int k = 1; k < arrPOSFile.Length; k++)
                            {
                                if (arrPOSFile[k].Contains(ColumnONE))
                                {
                                    string temptopbottom = arrPOSFile[k];
                                    TOPBOTTOMValue = temptopbottom.Split(';')[1].ToUpper();
                                    break;
                                }
                            }
                            if (TOPBOTTOMValue == dtop)
                            {
                                temp = iBomType.Substring(0, 2) + datafieldindex[fieldindex] + "-" + dtopbottom[0].Split(':')[1];
                            }
                            else if (TOPBOTTOMValue == dbottom)
                            {
                                temp = iBomType.Substring(0, 2) + datafieldindex[fieldindex] + "-" + dtopbottom[1].Split(':')[1];
                            }

                            ss.ParameterValue = temp;
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    pd.listSSD.Add(ss);
                }
                iSeqNo++;
                listSSD.Add(pd);
            }
            return listSSD;
        }

        public IList<BOMParameterDao> AnalysisHeadFile(string[] arrSourceFile, string[] arrPOSFile, string BOMType)
        {
            //初始化ValueIndex和HeadIndex
            BOM_Head btt = CommonBLL.getBOM_HeadByBOMType(BOMType);
            iBomType = string.IsNullOrWhiteSpace(btt.BOMType.ToString()) ? iBomType : btt.BOMType;
            iDataFieldIdx = string.IsNullOrWhiteSpace(btt.DataFieldIdx.ToString()) ? iDataFieldIdx : btt.DataFieldIdx;
            //iuserdefine1 = string.IsNullOrWhiteSpace(btt.userDefine1.ToString()) ? iuserdefine1 : btt.userDefine1;
            //iuserdefine2 = string.IsNullOrWhiteSpace(btt.userDefine2.ToString()) ? iuserdefine2 : btt.userDefine2;
            //iuserdefine3 = string.IsNullOrWhiteSpace(btt.userDefine3.ToString()) ? iuserdefine3 : btt.userDefine3;
            //iuserdefine4 = string.IsNullOrWhiteSpace(btt.userDefine4.ToString()) ? iuserdefine4 : btt.userDefine4;
            //iuserdefine5 = string.IsNullOrWhiteSpace(btt.userDefine5.ToString()) ? iuserdefine5 : btt.userDefine5;
            iuserdefine = string.IsNullOrWhiteSpace(btt.SpecialFlag.ToString()) ? iuserdefine : btt.SpecialFlag;
            string[] ArrIuserdefine = iuserdefine.Split(':');

            iuserdefine1 = ArrIuserdefine[0];
            iuserdefine2 = ArrIuserdefine[1];
            iuserdefine3 = ArrIuserdefine[2];
            iuserdefine4 = ArrIuserdefine[3];
            iuserdefine5 = ArrIuserdefine[4];
            //TestparamDAL tpDAL = new TestparamDAL();
            IList<BOMParameterDao> listSSDA = new List<BOMParameterDao>();
            //12个
            IQueryable<BOM_Detail> listBT = new BaseRepositoryDAL<BOM_Detail>().LoadEntities(p => p.BOMType.Equals(BOMType, StringComparison.OrdinalIgnoreCase)).OrderBy(P => P.Seq);
            //listBT.OrderBy(P => P.Seq);
            //  string sequence = new BaseRepositoryDAL<bas_TestType>().LoadEntities(p => p.testType.Equals(BOMType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().dataFieldIndex;
            //tpDAL.GetTestParamBytestType(fileType);
            BOMParameterDao pda = new BOMParameterDao();
            int fieldindex = -1;
            string PN = "";

            string datafieldindex = arrSourceFile[Convert.ToInt32(iDataFieldIdx)];
            string datafieldindexAdd = arrSourceFile[Convert.ToInt32(iDataFieldIdx)+1];
            //ExcelExport.WriteLog(logPath, datafieldindex + "datafieldindex431");
            //ExcelExport.WriteLog(logPath, datafieldindexAdd + "datafieldindexAdd431");    
            string[] temdatafieldindex = datafieldindex.Split(';');
            string[] temdatafieldindexAdd = datafieldindexAdd.Split(';');
            for (int i = 0; i< temdatafieldindex.Length; i++)
            {
                BOMParameter ssa = new BOMParameter();
                //ExcelExport.WriteLog(logPath, temdatafieldindex[i].Trim() + iuserdefine1.Trim()+"431");
                if (temdatafieldindex[i].Trim() == iuserdefine1.Trim())
                {
                    fieldindex = i + 1;
                    PN = temdatafieldindex[fieldindex];
                    ssa.ParameterName = iuserdefine1;
                    ssa.ParameterValue = PN;
                    //ExcelExport.WriteLog(logPath, "产生头文件431");
                    pda.listSSD.Add(ssa);
                    break;
                }
            }
            for (int i = 0; i < temdatafieldindex.Length; i++)
            {
                BOMParameter ssa = new BOMParameter();
                if (temdatafieldindex[i].Trim() == iuserdefine2.Trim())
                {
                    fieldindex = i + 1;
                    PN = temdatafieldindex[fieldindex];
                    ssa.ParameterName = iuserdefine2;
                    ssa.ParameterValue = PN;
                    //ExcelExport.WriteLog(logPath, "产生头文件445");
                    pda.listSSD.Add(ssa);
                    break;
                }
            }
            for (int i = 0; i < temdatafieldindexAdd.Length; i++)
            {
                BOMParameter ssa = new BOMParameter();
                //ExcelExport.WriteLog(logPath, temdatafieldindexAdd[i].Trim() + iuserdefine3.Trim()+"459");
                if (temdatafieldindexAdd[i].Trim() == iuserdefine3.Trim())
                {
                    fieldindex = i + 1;
                    PN = temdatafieldindexAdd[fieldindex];
                    ssa.ParameterName = iuserdefine3;
                    ssa.ParameterValue = PN;
                    //ExcelExport.WriteLog(logPath, "产生头文件459");
                    pda.listSSD.Add(ssa);
                    break;
                }
            }
            for (int i = 0; i < temdatafieldindexAdd.Length; i++)
            {
                BOMParameter ssa = new BOMParameter();
                if (temdatafieldindexAdd[i].Trim() == iuserdefine4.Trim())
                {
                    fieldindex = i + 1;
                    PN = temdatafieldindexAdd[fieldindex];
                    ssa.ParameterName = iuserdefine4;
                    ssa.ParameterValue = PN;
                    //ExcelExport.WriteLog(logPath, "产生头文件473");
                    pda.listSSD.Add(ssa);
                    break;
                }
            }
              listSSDA.Add(pda);
            return listSSDA;
        }
        public IList<BOMParameterDao> AnalysisHead(string BOMType)
        {
            //初始化ValueIndex和HeadIndex
            BOM_Head btt = CommonBLL.getBOM_HeadByBOMType(BOMType);
            iBomType = string.IsNullOrWhiteSpace(btt.BOMType.ToString()) ? iBomType : btt.BOMType;
            iDataFieldIdx = string.IsNullOrWhiteSpace(btt.DataFieldIdx.ToString()) ? iDataFieldIdx : btt.DataFieldIdx;
            icustomermark = string.IsNullOrWhiteSpace(btt.CustomerMark.ToString()) ? icustomermark : btt.CustomerMark;
            iuserdefine = string.IsNullOrWhiteSpace(btt.SpecialFlag.ToString()) ? iuserdefine : btt.SpecialFlag;
            string[] ArrIuserdefine = iuserdefine.Split(':');

            //iuserdefine6 = string.IsNullOrWhiteSpace(btt.userDefine6.ToString()) ? iuserdefine6 : btt.userDefine6;
            //iuserdefine7 = string.IsNullOrWhiteSpace(btt.userDefine7.ToString()) ? iuserdefine7 : btt.userDefine7;
            //iuserdefine8 = string.IsNullOrWhiteSpace(btt.userDefine8.ToString()) ? iuserdefine8 : btt.userDefine8;
            //iuserdefine9 = string.IsNullOrWhiteSpace(btt.userDefine9.ToString()) ? iuserdefine9 : btt.userDefine9;
            //iuserdefine10 = string.IsNullOrWhiteSpace(btt.userDefine10.ToString()) ? iuserdefine10 : btt.userDefine10;
            //iuserdefine11 = string.IsNullOrWhiteSpace(btt.userDefine11.ToString()) ? iuserdefine11 : btt.userDefine11;
            //iuserdefine12 = string.IsNullOrWhiteSpace(btt.userDefine12.ToString()) ? iuserdefine12 : btt.userDefine12;
            //iuserdefine13 = string.IsNullOrWhiteSpace(btt.userDefine13.ToString()) ? iuserdefine13 : btt.userDefine13;
            //iuserdefine14 = string.IsNullOrWhiteSpace(btt.userDefine14.ToString()) ? iuserdefine14 : btt.userDefine14;
            //iuserdefine15 = string.IsNullOrWhiteSpace(btt.userDefine15.ToString()) ? iuserdefine15 : btt.userDefine15;
            //iuserdefine16 = string.IsNullOrWhiteSpace(btt.userDefine16.ToString()) ? iuserdefine16 : btt.userDefine16;

            iuserdefine6 = ArrIuserdefine[5];
            iuserdefine7 = ArrIuserdefine[6];
            iuserdefine8 = ArrIuserdefine[7];
            iuserdefine9 = ArrIuserdefine[8];
            iuserdefine10 = ArrIuserdefine[9];
            iuserdefine11 = ArrIuserdefine[10];
            iuserdefine12 = ArrIuserdefine[11];
            iuserdefine13 = ArrIuserdefine[12];
            iuserdefine14 = ArrIuserdefine[13];
            iuserdefine15 = ArrIuserdefine[14];
            iuserdefine16 = ArrIuserdefine[15];
            IList<BOMParameterDao> listSSDHead = new List<BOMParameterDao>();
            //12个
            IQueryable<BOM_Detail> listBT = new BaseRepositoryDAL<BOM_Detail>().LoadEntities(p => p.BOMType.Equals(BOMType, StringComparison.OrdinalIgnoreCase)).OrderBy(P => P.Seq);
            //listBT.OrderBy(P => P.Seq);
            //  string sequence = new BaseRepositoryDAL<bas_TestType>().LoadEntities(p => p.testType.Equals(BOMType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().dataFieldIndex;
            //tpDAL.GetTestParamBytestType(fileType);
            BOMParameterDao pda = new BOMParameterDao();
            BOMParameter ssa = new BOMParameter();
            ssa.ParameterName = "CustomerMark";
            ssa.ParameterValue = icustomermark;
            pda.listSSD.Add(ssa);
            BOMParameter ssa6 = new BOMParameter();
            ssa6.ParameterName = "userdefine6";
            ssa6.ParameterValue = iuserdefine6;
            pda.listSSD.Add(ssa6);
            BOMParameter ssa7 = new BOMParameter();
            ssa7.ParameterName = "userdefine7";
            ssa7.ParameterValue = iuserdefine7;
            pda.listSSD.Add(ssa7);
            BOMParameter ssa8 = new BOMParameter();
            ssa8.ParameterName = "userdefine8";
            ssa8.ParameterValue = iuserdefine8;
            pda.listSSD.Add(ssa8);
            BOMParameter ssa9 = new BOMParameter();
            ssa9.ParameterName = "userdefine9";
            ssa9.ParameterValue = iuserdefine9;
            pda.listSSD.Add(ssa9);
            BOMParameter ssa10 = new BOMParameter();
            ssa10.ParameterName = "userdefine10";
            ssa10.ParameterValue = iuserdefine10;
            pda.listSSD.Add(ssa10);
            BOMParameter ssa11 = new BOMParameter();
            ssa11.ParameterName = "userdefine11";
            ssa11.ParameterValue = iuserdefine11;
            pda.listSSD.Add(ssa11);
            BOMParameter ssa12 = new BOMParameter();
            ssa12.ParameterName = "userdefine12";
            ssa12.ParameterValue = iuserdefine12;
            pda.listSSD.Add(ssa12);
            BOMParameter ssa13 = new BOMParameter();
            ssa13.ParameterName = "userdefine13";
            ssa13.ParameterValue = iuserdefine13;
            pda.listSSD.Add(ssa13);
            BOMParameter ssa14 = new BOMParameter();
            ssa14.ParameterName = "userdefine14";
            ssa14.ParameterValue = iuserdefine14;
            pda.listSSD.Add(ssa14);
            BOMParameter ssa15 = new BOMParameter();
            ssa15.ParameterName = "userdefine15";
            ssa15.ParameterValue = iuserdefine15;
            pda.listSSD.Add(ssa15);
            BOMParameter ssa16 = new BOMParameter();
            ssa16.ParameterName = "userdefine16";
            ssa16.ParameterValue = iuserdefine16;
            pda.listSSD.Add(ssa16);

            listSSDHead.Add(pda);
            return listSSDHead;
        }

        /// <summary>
        /// 按列名查找序列号
        /// </summary>
        /// <param name="str_row">列名集合</param>
        /// <param name="search_parameter">查找的列名</param>
        /// <returns></returns>
        protected virtual int Get_Parameter_Position(string str_row, string search_parameter)
        {
            int pos = -1;
            string[] strs = str_row.Trim().Split(',');

            for (int k = 0; k < strs.Length; k++)
            {
                if (strs[k].Trim().Equals(search_parameter, StringComparison.OrdinalIgnoreCase))
                {
                    pos = k;
                    break;
                }
            }
            return pos;
        }

    }
}
